package com.ruoyi.common.utils.msg.util.format;

public class WindDirectionUtil {


    /**
     * 将String类型的角度转换为风向名称
     *
     * @param degreeStr 风向角度字符串(0-360度)
     * @return 风向名称
     */
    public static String degreeToDirection(String degreeStr) {
        if (degreeStr == null || degreeStr.trim().isEmpty()) {
            return "0";
        }
        double degree = parseDegree(degreeStr);
        return degreeToDirection(degree);
    }

    /**
     * 解析String类型的风向角度数据
     *
     * @param degreeStr 风向角度字符串
     * @return 解析后的角度值
     */
    private static double parseDegree(String degreeStr) {
        if (degreeStr == null || degreeStr.trim().isEmpty()) {
            throw new IllegalArgumentException("风向角度字符串不能为空");
        }

        try {
            double degree = Double.parseDouble(degreeStr.trim());
            if (degree < 0 || degree > 360) {
                // 处理超出范围的角度值，将其转换为0-360范围内
                degree = (degree % 360 + 360) % 360;
            }
            return degree;
        } catch (NumberFormatException e) {
            throw new IllegalArgumentException("风向角度字符串格式不正确: " + degreeStr, e);
        }
    }

    /**
     * 将角度转换为风向名称
     *
     * @param degree 风向角度(0-360度)
     * @return 风向名称
     */
    public static String degreeToDirection(double degree) {
        if (degree < 0){
            degree = 360 + degree;
        }else if (degree > 360){
            degree = degree - 360;
        }
        if (degree < 0 || degree > 360) {
            throw new IllegalArgumentException("风向角度必须在0-360度之间");
        }

        // 8个基本风向
        if (degree >= 337.5 || degree < 22.5) return "北";
        if (degree >= 22.5 && degree < 67.5) return "东北";
        if (degree >= 67.5 && degree < 112.5) return "东";
        if (degree >= 112.5 && degree < 157.5) return "东南";
        if (degree >= 157.5 && degree < 202.5) return "南";
        if (degree >= 202.5 && degree < 247.5) return "西南";
        if (degree >= 247.5 && degree < 292.5) return "西";
        return "西北";

//        // 16个基本风向
//        if (degree >= 348.75 || degree < 11.25) return "北";
//        if (degree >= 11.25 && degree < 33.75) return "北东北";
//        if (degree >= 33.75 && degree < 56.25) return "东北";
//        if (degree >= 56.25 && degree < 78.75) return "东东北";
//        if (degree >= 78.75 && degree < 101.25) return "东";
//        if (degree >= 101.25 && degree < 123.75) return "东东南";
//        if (degree >= 123.75 && degree < 146.25) return "东南";
//        if (degree >= 146.25 && degree < 168.75) return "南东南";
//        if (degree >= 168.75 && degree < 191.25) return "南";
//        if (degree >= 191.25 && degree < 213.75) return "南西南";
//        if (degree >= 213.75 && degree < 236.25) return "西南";
//        if (degree >= 236.25 && degree < 258.75) return "西西南";
//        if (degree >= 258.75 && degree < 281.25) return "西";
//        if (degree >= 281.25 && degree < 303.75) return "西西北";
//        if (degree >= 303.75 && degree < 326.25) return "西北";
//        return "北西北";
    }
}
